/*************************************************************************
	> File Name: id1.c
	> Author: Angel Criss
	> Mail: abbytsing@gmail.com 
	> Created Time: 日  3/30 15:36:50 2014
 ************************************************************************/

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<signal.h>
#include<stdlib.h>
#include<sys/wait.h>
int main(int argc,char *argv[])
{
    if(argc<2)
    {
        printf("run by: ./a.out filename\n");
        exit(0);
    }
    else
    {
        printf("the ppid is %ld\nthen finish access in child process\n",(long)getppid());

        pid_t pid;
        pid=fork();
        if(pid<0)
        {
            perror("fork()");
            exit(0);
        }
        if(pid==0)
        {
            printf("the child pid is: %ld\n ",(long)getpid());
            printf("in this process test if a file readable\n");
            if(access(argv[1],R_OK)!=-1)
                printf("this file can be read\n");
            else
            {
                printf("you cannot read this file\n");
                _exit(0);
            }
            printf("all done then sleep 10s\n");
            sleep(60);
            _exit(0);
        }
        else if(pid>0)
        {
            pid_t still;
            printf("now ppid again: %ld",(long)getppid());
            still=wait(NULL);
            printf("now wait for child process exit\n");
            printf("now exit by kill the parent process\n");
            kill(getppid(),SIGINT);
            exit(0);
        }
    }
}
